<?php
class Pauler_Searchseo_Model_Observer extends Mage_Core_Model_Abstract{

    /*
     * controller_front_send_response_before
     */
    public function test($observer){
        //  echo 'tt';exit;
        $front = $observer->getEvent()->getFront(); 
    }
    /*
     * controller_front_send_response_before
     */
    public function resultProduct($observer){

        $ControllerAction = $observer->getEvent()->getControllerAction();
        $catalogsearchquery = Mage::helper('catalogsearch')->getQuery();

        //echo $catalogsearchquery->getQueryText();
        if ($catalogsearchquery->getQueryText()) {

            //  if (Mage::helper('catalogsearch')->isMinQueryLength()) {

            //  }
            // else {
            //  if ($query->getId()) {
            //      $query->setPopularity($query->getPopularity()+1);
            //   }
            //  else {
            //       $query->setPopularity(1);
            //   }

            //  if ($query->getRedirect()){
            // $query->save();
            // $this->getResponse()->setRedirect($query->getRedirect());
            // return;
            //  }
            //  else {
            //     $query->prepare();
            // }
            // }          

            if (!Mage::helper('catalogsearch')->isMinQueryLength()) {
                $query = Mage::getModel('searchseo/query')->loadByQueryText($catalogsearchquery->getQueryText());
                $query->setStoreId(Mage::app()->getStore()->getId());
                $layer = Mage::registry('current_layer');
                    if (!$layer) {
                        $layer =  Mage::getSingleton('catalogsearch/layer');
                    }
         
               // var_dump($layer->getProductCollection()->getSize());
                //$query->setNumResults($layer->getProductCollection()->getSize());
               // var_dump($catalogsearchquery->getNumResults());  
                $query->setNumResults($catalogsearchquery->getNumResults());
                if (!$query->getId()) {
                    $query->setQueryText($catalogsearchquery->getQueryText());
                }
                if ($query->getId()) {
                    $query->setPopularity($query->getPopularity()+1);
                }
                else {
                    $query->setPopularity(1);
                }
                
                if($query->getRedirect()){
                    $query->save();
                    
                    if($query->getIsActive()){
                        $category = Mage::getModel('catalog/category')->load($query->getRedirect());
                        if( $category->getIsActive()){
                            $ControllerAction->getResponse()->setRedirect($category->getUrl());
                        }
                    }
                }
                if(1==$query->getNumResults()){
                    

                    //var_dump(count(Mage::getSingleton('catalog/layer')->getProductCollection()));
                    if( $layer->getProductCollection() && $product = $layer->getProductCollection()->getFirstItem()){
                           
                        if($product->getProductUrl()) {
                            $query->setUrl($product->getProductUrl());
                            $query->save();
                            $ControllerAction->getResponse()->setRedirect($product->getProductUrl());
                        }
                    }
                    // $ControllerAction->getResponse()->setRedirect(Mage::getUrl().$query->getRedirect());
                }
                $query->save();
            }
        }
        //exit;
        //echo get_class($ControllerAction); 
    }
    public function getContent($observer){
      /* $action = $observer->getEvent()->getControllerAction();
       //$ControllerAction->getRequest()->isDispatched() ;
       $request = $action->getRequest();
       $action->getLayout()->getUpdate()
            ->addHandle('default')
            ->addHandle('cms_index_noroute'); 
       //$action->addActionLayoutHandles();
        if (false) {
            $handle = ($page->getCustomRootTemplate()
                        && $page->getCustomRootTemplate() != 'empty'
                        && $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
            $action->getLayout()->helper('page/layout')->applyHandle($handle);
        }
       $action->loadLayout();
       //$action->loadLayoutUpdates();
       //$action->generateLayoutXml()->generateLayoutBlocks();
       $action->renderLayout();
       $action->setFlag('',Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH,'true');
      //var_dump(!$action->getFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH) );*/

    }
}
?>
